package cn.sc.allinpay.allfavour.module.printer;

import com.landicorp.android.eptapi.device.Printer;

import cn.pai.common.utils.AmtUtils;
import cn.sc.allinpay.allfavour.business.common.data.SettleData;

/**
 * 收银结算打印
 * author：pany
 * on 2017/11/3 11:48
 */
public class SettlePrinter extends BasePrinter {

    public SettlePrinter(PrintData data) {
        super(data);
    }

    @Override
    protected void printBody(Printer printer, PrintData data) throws Exception {
        printer.setFormat(FormatStyle.createNormalFont());
        printer.println("开始时间：" + data.getSettleTime());
        printer.println("结束时间：" + data.getSettleTime2());
        printer.println(formatString("消费：") + formatString(data.getSaleNum()) + data.getSale());
        printer.println(formatString("撤销：") + formatString(data.getVoidNum()) + data.getVoidAmt());
        printer.println(formatString("实收：") + formatString("") + data.getPaidAmt());
        printer.println("...........................");
        printer.println(formatString("优惠：") + data.getMemberDisAmt());
        printer.println(formatString("用券：") + data.getCouponAmt());
        printer.println(formatString("钱包：") + data.getWalletAmt());
        printer.println(formatString("现金：") + data.getCash());
        printer.println(formatString("刷卡扫码：") + data.getOnlineAmt());
        printer.println("...........................");
        for (SettleData settleData : data.getSettleDatas()) {
            String name = settleData.getOilName() + "：";
            printer.println(formatString(name) + formatString((settleData.getSaleNum() - settleData.getVoidNum()) + "笔") + AmtUtils.formatY(settleData.getSaleAmt().subtract(settleData.getVoidAmt())));
            if (settleData.getOilName().equals("CNG")) {
                printer.println(formatString("小计：") + formatString(data.getTotalNum()) + data.getTotalAmt());
            }
        }
        printer.println("");
        printer.println("...........................");
        printer.feedLine(4);
    }

    @Override
    protected void printFooter(Printer printer, PrintData data) throws Exception {
        printer.feedLine(4);
    }

    public static String formatString(String str) {
        int length = 12;
        String slot = " ";
        StringBuffer sb = new StringBuffer();
        sb.append(str);

        int count = length - length(str);

        while (count > 0) {
            sb.append(slot);
            count--;
        }

        return sb.toString();
    }

    public static int length(String value) {
        int valueLength = 0;
        String chinese = "[\u0391-\uFFE5]";
        /* 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1 */
        for (int i = 0; i < value.length(); i++) {
            /* 获取一个字符 */
            String temp = value.substring(i, i + 1);
            /* 判断是否为中文字符 */
            if (temp.matches(chinese)) {
                /* 中文字符长度为2 */
                valueLength += 2;
            } else {
                /* 其他字符长度为1 */
                valueLength += 1;
            }
        }
        return valueLength;
    }
}
